package org.commonsemantics.scigrails.module.users

import grails.converters.deep.* 
import org.commonsemantics.scigrails.module.users.circle.UserCircle 
import org.commonsemantics.scigrails.module.users.community.UserCommunity 
import org.commonsemantics.scigrails.module.users.group.Group 
import org.commonsemantics.scigrails.module.users.group.UserGroup 
import org.commonsemantics.scigrails.module.users.security.User 

class UsersAjaxController {

	def springSecurityService
	
	private def injectUserProfile() {
		def principal = springSecurityService.principal
		String username = principal.username
		def user = User.findByUsername(username);
		//TODO Manage exceptions
		user
	}
	
	// USER
	//--------------------------------
	
	// GROUPS
	//--------------------------------
	/*
	 * Pass through method that extracts the id parameter
	 * of the user and returns hers UserGroup entities.
	 */
	def userGroups = {
		return getUserGroups(User.findById(params.id));
	}
	
	/*
	 * This returns UserGroup entities as that makes possible
	 * retrieving the details for this relationship and both
	 * the user and the group data
	 */
	def getUserGroups(def user) {
		def userGroups = []
		userGroups = UserGroup.findAllByUser(user)
		render userGroups as JSON;
	}

	def searchGroups = {
		searchUserGroups(User.findById(params.id));
	}
	
	def searchUserGroups(def user) {
		def groups = []
		groups = Group.findAll()
		groups.each { 
			println "Before " + it.membersCounter
			it.setMembersCounter("0") 
			println "After " + it.membersCounter
		}
		render groups as JSON;
	}
	
	// CIRCLES
	//--------------------------------
	/*
	* Pass through method that extracts the id parameter
	* of the user and returns hers UserCircle entities.
	*/
	def userCircles = {
		getUserCircles(User.findById(params.id));
	}
	
	/*
	* This returns UserCircle entities as that makes possible
	* retrieving the details for this relationship and both
	* the user and the circle data
	*/
	def getUserCircles(def user) {
		def userCircles = []
		userCircles = UserCircle.findAllByUser(user)
		render userCircles as JSON;
	}
	
	// COMMUNITIES
	//--------------------------------
	/*
	* Pass through method that extracts the id parameter
	* of the user and returns hers UserCommunity entities.
	*/
	def userCommunities = {
		getUserCommunities(User.findById(params.id));
	}
	
	/*
	* This returns UserCommunity entities as that makes possible
	* retrieving the details for this relationship and both
	* the user and the community data
	*/
	def getUserCommunities(def user) {
		def userCommunities = []
		userCommunities = UserCommunity.findAllByUser(user)
		render userCommunities as JSON;
	}
	
	def searchCommunities = {
		searchUserCommunities(User.findById(params.id));
	}
	
	def searchUserCommunities(def user) {
		def userCommunities = []
		userCommunities = UserCommunity.findAllByUser(user)
		def results = [communities: userCommunities, communitiesCount: userCommunities.size()]
		userCommunities.each { println it.community.name }
		println results
		render userCommunities as JSON;
	}
	
	def searchCircles = {
		searchUserCircles(User.findById(params.id));
	}
	
	def searchUserCircles(def user) {
		def userCircles = []
		userCircles = UserCircle.findAllByUser(user)
		def results = [circles: userCircles, circlesCount: userCircles.size()]
		userCircles.each { println it.circle.name }
		println results
		render userCircles as JSON;
	}
}
